package com.chag.biz.userbase.service;

import com.chag.biz.userbase.dao.customized.UserCustomizedMapper;
import com.chag.biz.userbase.dao.gen.UserGeneratedMapper;
import com.chag.biz.userbase.dto.UserDTO;
import com.chag.biz.userbase.dto.UserSearchDTO;
import com.chag.biz.userbase.dto.UserStatusEnum;
import com.chag.biz.userbase.entity.gen.UserGenerated;
import com.chag.framework.common.ServiceResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: CHAG
 * Date: 2016/6/3
 * Time: 22:41
 * 用户服务接口实现类.
 */
public class UserServiceImpl implements IUserService {
    @Resource
    private UserGeneratedMapper userGeneratedMapper;
    @Resource
    private UserCustomizedMapper userCustomizedMapper;

    /**
     * 创建用户
     *
     * @param userDTO 用户对象
     * @return
     */
    public ServiceResult<UserDTO> createUser(UserDTO userDTO) {
        if (userDTO == null || StringUtils.isBlank(userDTO.getName()) || StringUtils.isBlank(userDTO.getOrgCode())) {
            return new ServiceResult<UserDTO>(Boolean.FALSE, "参数错误！");
        }

        UserGenerated userGenerated = new UserGenerated();

        BeanUtils.copyProperties(userDTO, userGenerated);
        userGenerated.setStatus(UserStatusEnum.ACTIVE.getStatusCode());

        userGeneratedMapper.insert(userGenerated);

        BeanUtils.copyProperties(userGenerated, userDTO);

        return new ServiceResult<UserDTO>(Boolean.TRUE, userDTO, "插入数据成功！");
    }

    /**
     * 获取用户
     *
     * @param userCode 用户编号
     * @return
     */
    public ServiceResult<UserDTO> getUserByCode(String userCode) {
        UserSearchDTO searchDTO = new UserSearchDTO();
        searchDTO.setCode(userCode);

        List<UserDTO> userDTOs = getUserListBySearchDTO(searchDTO).getData();

        if (!CollectionUtils.isEmpty(userDTOs)) {
            return new ServiceResult<UserDTO>(Boolean.TRUE, userDTOs.get(0), "查询成功！");
        }

        return new ServiceResult<UserDTO>(Boolean.FALSE, "未查询到数据！");
    }

    /**
     * 根据id获取用户
     *
     * @param id 用户id
     * @return
     */
    public ServiceResult<UserDTO> getUserById(String id) {
        UserSearchDTO searchDTO = new UserSearchDTO();
        searchDTO.setId(id);

        List<UserDTO> userDTOs = getUserListBySearchDTO(searchDTO).getData();

        if (!CollectionUtils.isEmpty(userDTOs)) {
            return new ServiceResult<UserDTO>(Boolean.TRUE, userDTOs.get(0), "查询成功！");
        }

        return new ServiceResult<UserDTO>(Boolean.FALSE, "未查询到数据！");
    }

    /**
     * 根据企业获取用户列表
     *
     * @param orgCode 企业编码
     * @param status  用户状态
     * @return
     */
    public ServiceResult<List<UserDTO>> getUserByOrgCode(String orgCode, UserStatusEnum... status) {
        UserSearchDTO searchDTO = new UserSearchDTO();
        searchDTO.setOrgCode(orgCode);

        List<Integer> statusList = new ArrayList<Integer>();
        for (UserStatusEnum statusEnum : status) {
            statusList.add(statusEnum.getStatusCode());
        }
        searchDTO.setStatusList(statusList);

        return getUserListBySearchDTO(searchDTO);
    }

    /**
     * 根据查询条件查询用户列表
     *
     * @param searchDTO 查询条件封装对象
     * @return
     */
    public ServiceResult<List<UserDTO>> getUserListBySearchDTO(UserSearchDTO searchDTO) {
        List<UserDTO> userDTOList = userCustomizedMapper.selectUserListBySearchDTO(searchDTO);

        return new ServiceResult<List<UserDTO>>(Boolean.TRUE, userDTOList, "查询成功！");
    }

    /**
     * 根据查询条件查询用户数量
     *
     * @param searchDTO 查询条件封装对象
     * @return
     */
    public ServiceResult<Integer> getUserCountBySearchDTO(UserSearchDTO searchDTO) {
        Integer count = userCustomizedMapper.selectUserCountBySearchDTO(searchDTO);

        return new ServiceResult<Integer>(Boolean.TRUE, -1, "查询成功！", count);
    }

    /**
     * 获取所有用户列表
     *
     * @param status 用户状态
     * @return
     */
    public ServiceResult<List<UserDTO>> getAllUser(UserStatusEnum... status) {
        UserSearchDTO searchDTO = new UserSearchDTO();
        List<Integer> statusList = new ArrayList<Integer>();

        for (UserStatusEnum statusEnum : status) {
            statusList.add(statusEnum.getStatusCode());
        }
        searchDTO.setStatusList(statusList);

        return getUserListBySearchDTO(searchDTO);
    }

    /**
     * 更新用户
     *
     * @param userDTO 用户对象
     * @return
     */
    public ServiceResult<UserDTO> updateUser(UserDTO userDTO) {
        UserGenerated userGenerated = new UserGenerated();
        BeanUtils.copyProperties(userDTO, userGenerated);

        userGeneratedMapper.updateByPrimaryKeySelective(userGenerated);

        return new ServiceResult<UserDTO>(Boolean.TRUE, userDTO, "更新数据成功！");
    }

    /**
     * 更新用户名称
     *
     * @param id   用户id
     * @param name 用户名称
     * @return
     */
    public ServiceResult<UserDTO> updateUserName(String id, String name) {
        if (StringUtils.isBlank(id) || StringUtils.isBlank(name)) {
            return new ServiceResult<UserDTO>(Boolean.FALSE, "参数错误！");
        }

        UserGenerated userGenerated = userGeneratedMapper.selectByPrimaryKey(id);

        if (userGenerated == null) {
            return new ServiceResult<UserDTO>(Boolean.FALSE, "用户不存在！");
        }

        userGenerated.setName(name);

        userGeneratedMapper.updateByPrimaryKeySelective(userGenerated);

        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(userGenerated, userDTO);

        return new ServiceResult<UserDTO>(Boolean.TRUE, userDTO, "更新数据成功！");
    }

    /**
     * 删除用户
     *
     * @param id 用户id
     * @return
     */
    public ServiceResult<Boolean> deleteUser(String id) {
        if (StringUtils.isBlank(id)) {
            return new ServiceResult<Boolean>(Boolean.FALSE, "参数错误！");
        }

        UserGenerated userGenerated = userGeneratedMapper.selectByPrimaryKey(id);

        if (userGenerated == null) {
            return new ServiceResult<Boolean>(Boolean.FALSE, "用户不存在！");
        }

        userGenerated.setStatus(UserStatusEnum.DELETE.getStatusCode());

        return new ServiceResult<Boolean>(Boolean.TRUE, Boolean.TRUE, "删除用户成功！");
    }
}
